<?php

namespace App\Console\Commands;

use App\Api\Logic\Curl;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use phpseclib3\Math\BigInteger\Engines\PHP;
use Rap2hpoutre\FastExcel\FastExcel;
use function Symfony\Component\String\s;

class DataRecallReg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dataRecallReg';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '通过 xlsx 文件重放请求';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $time = date("Y-m-d H:i:s",time());
        $data = $this->getData('first_entry_time_new');
//        $openapi_url = 'https://openapi.lecoosys.com';
//        $service_url = 'https://service.lecoosys.com';

        $openapi_url = '127.0.0.1:8002';
//        $service_url = '127.0.0.1:8001';
        $curl = new Curl();
        $curl->SetHeader('Content-Type','application/json');
        $curl->SetHeader('AUTHORIZATION','Bearer ' . '5f7ae63036f59cd9726b2c1a02dfb602360016f6');
        foreach ($data as $k=>$requestParam){
            $response = [];
            $record = [];
            if(strpos($requestParam['uri'], "openapi")){
                $aa = parse_url($requestParam['uri']);
                $bb = explode("&", $aa['query']);
                $qqData = [];
                foreach ($bb as $k=>$v){
                    $str = explode('=', $v);
                    $qqData[$str[0]] = $str[1];
                }
                $re = false;
                $str_time = str_replace("+", " ",$qqData['timestamp']);
                $time = strtotime($str_time);
                $f_data = [];
                $pa = json_decode($requestParam['content'], true);
                $checkRe = DB::table('tmall_user')->where(['mix_mobile' => $pa['mix_mobile']])->first();
                //修复没有ouid的历史数据
                if(empty($checkRe->ouid)){
                    $f_data['ouid'] = $pa['ouid'] ?? '';
                }
                if(empty($checkRe->open_id)){
                    $f_data['open_id'] = $pa['open_id'] ?? '';
                }
                if(empty($checkRe->first_entry_time)){
                    $f_data['first_entry_time'] = $time * 1000;
                    $f_data['last_entry_time'] = $time * 1000;
                } else if(!empty($checkRe->first_entry_time)){
                    $f_data['last_entry_time'] = $time * 1000;
                }
                if(!empty($f_data)) {
                   $re =  DB::table('tmall_user')->where(['mix_mobile' => $pa['mix_mobile']])->update($f_data);
                }
                if($re){
                    echo '成功';
                } else{
                    echo '失败';
                }
//                $response = $curl->Post($openapi_url.$requestParam['uri'], json_decode($requestParam['request_body'], true));
//                $this->record($openapi_url.$requestParam['uri'],$requestParam,$response, $time);
//                $this->line($response);
            }
//            $response = $curl->Post($service_url.$requestParam['url_path'], $requestParam['request_body']);
//            $this->record($openapi_url,$requestParam,$response);
//            $this->line($response);
        }
        return 0;
    }

    protected function getData($name){
        $collection = (new FastExcel())->import(storage_path()."/".$name.".xlsx");
        return $collection;
    }

    public function record($openapi_url,$requestParam,$response, $time){
        $record['msg'] = $response;
        $record['host'] = $openapi_url;
        $record['request'] = json_encode(json_decode($requestParam['request_body'], true));
        $record['status'] = 200;
        $record['url'] = $requestParam['uri'];
        $record['created_at'] = $time;
        return DB::table('exec_record_reg')->insertGetId($record);
    }
}
